<?php
/**
 * 商品列表控制器
 * 
 * @version $Id: Shops.php 652 2013-01-23 07:52:01Z linsir123 $
 */
class controllerShops extends TaobaokeController 
{
	/**
	 * @var 模型
	 */
	public $mShop;
	
	/**
	 * @var 模型
	 */
	public $mClass;
	
	/**
	 * @var 分类
	 */
	public $class;
	
	/**
	 * @var 分类
	 */
	public $navs;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		parent::__construct();
		
		///
		$this->mShop = M::shop();
		$this->mClass = M::classes();
		
		///
		$this->navs = $this->mClass->getShop();
		
		///
		$this->listType = 2;
		$this->taobaoke = array(
			'show'=>48,
			'sortField'=>'total_auction',
			'sortType'=>'desc',
		);
		$this->sorts = array(
			array('默认', 'total_auction'),
			array('热销', 'auction_count'),
			array('推荐', 'commission_rate'),
		);
		
		///
		$this->_init();
		$this->_checkCompatible();
	}
	
	/**
	 * 淘宝推广页
	 */
	public function actionIndex()
	{
		///
		$data = $this->mShop->getTaobaokes($this->taobaoke, true);
		list($list, $totalPage, $total) = $data;
		
		///
		AppView::addCss('themes/01/shops.css');
		View::display('shops/index');
		View::assignSet(array(
			'category' => $this->class, 
			'classes' => $this->navs, 
			
			///
			'total' => $total, 
			'list' => $list, 
			'sortInfo' => $this->_aSortInfo(), 
			'pagination' => $this->_aPagination($totalPage), 
		));
		$this->_assignPage($list);
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 初始化
	 */
	private function _init()
	{
		$keys = array(
			'pinyin' => 'trim',
			'cid' => 'intval',
			'pinyin_keyword' => 'trim',
			'keyword' => 'trim',
			'sort' => 'intval',
			'p' => 'intval',
		);
		$keysTaobaoke = array('cid', 'keyword', 'sort', 'p');
		foreach ($keys as $k => $m) {
			$v = call_user_func_array($m, array($_GET[$k]));
			$this->data[$k] = $v;
			if ( ! empty($v) && in_array($k, $keysTaobaoke)) {
				/// 1
				if ($k == 'sort') 
					$this->taobaoke['sortField'] = $this->sorts[$v][1];
				else 
					$this->taobaoke[$k] = $v;
			}
		}
		
		///
		if ( ! empty($this->data['pinyin_keyword'])) {
			$v = $this->mTerm->get($this->data['pinyin_keyword']);
			if ( ! empty($v)) {
				$this->data['keyword'] = $this->taobaoke['keyword'] = $v;
				$this->cleanKeyword = true;
			}
		}
	}
	
	/**
	 * 兼容性
	 */
	private function _checkCompatible()
	{
		$cId = $this->data['cid'];
		$keyword = $this->data['keyword'];
		$pinyin = $this->data['pinyin'];
		$pinyinKeyword = $this->data['pinyin_keyword'];
		
		/// 
		$params = array();
		if ($this->data['sort'] >= count($this->sorts)) 
			$params['sort'] = 0;
			
		/// 
		if ( ! empty($cId)) {
			foreach ($this->navs as $v) {
				if ($v['id'] == $cId) {
					$this->class = $v;
					break;
				}
			}
			if (empty($this->class)) {
				$params['pinyin'] = 'nvzhuangliuxingnvzhuang';
				$params['cid'] = 14;
			} else {
				if ($pinyin != $this->class['pinyin']) 
					$params['pinyin'] = $this->class['pinyin'];
			}
		} else {
			if (empty($keyword) || ! empty($pinyin)) {
				$params['pinyin'] = 'nvzhuangliuxingnvzhuang';
				$params['cid'] = 14;
			}
		}
		
		/// 1 关键字
		if ($this->cleanKeyword && ! empty($_GET['keyword'])) {
			$params['keyword'] = 0;
			$this->_h301($params);
		}
		
		/// 2 关键字
		if ( ! empty($keyword)) {
			App::V('pinyin');
			$tmp = string2pinyin($keyword);
			if ($tmp != $pinyinKeyword) {
				$params['pinyin_keyword'] = $tmp;
				$this->_h301($params);
			}
		}
		
		/// 3 其它
		if ( ! empty($params)) 
			$this->_h301($params);
	}
	
	/**
	 * 转换到视图层
	 */
	private function _assignPage($list)
	{
		$page = array('title'=>'','keywords'=>'','description'=>'');
		$keyword = empty($this->data['keyword']) ? 
			$this->class['title'] : $this->data['keyword'];
			
		/// title
		$page['title'] = $keyword;
		
		if ($this->data['p'] > 1) 
			$page['title'] .= '(第'. $this->data['p'] .'页)';
			
		$page['title'] .= sprintf(",淘宝网%s,淘宝网%s店铺,淘宝网%s品牌", 
			$keyword, $keyword, $keyword);
			
		/// keywords
		$page['keywords'] = "淘宝网,淘宝商城,天猫商城,". $keyword ."品牌汇";
		
		/// description
		$i = 0;
		$tmp = array();
		if (is_array($list[0])) {
			foreach ($list[0] as $v) {
				if ($i++ > 4) 
					break;
					
				///
				$tmp[] = $v['shop_title'];
			}
		}
		
		array_unshift($tmp, $keyword);
		
		$page['description'] = "www.e35go.com为您推荐淘宝网". implode(',', $tmp) ."人气商品,收藏本站方便您下次访问!";
		
		///
		View::assignSet(array(
			'page' => $page, 
			'keyword' => empty($this->data['keyword'])?"":$this->data['keyword'], 
		));
	}
}
